import { BaseWindow } from "../../../../component/window/BaseWindow";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { ObjCreate } from "../../common/ObjCreate";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { PetModelPanel } from "../PetModelPanel";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { TilePanel } from "../../equipment/TilePanel";
import { PetRecoverListRenderer } from "./PetRecoverListRenderer";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { PetUtil } from "../info/PetUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { DataEvent } from "../../../events/DataEvent";
import { SPetUpdate } from "../../../../../Message/Game/SPetUpdate";
import { Cache } from "../../../cache/Cache";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { GButton } from "../../../../../com/mui/controls/GButton";
type int = number;
//class PetRecoverWindow
    
    export  class PetRecoverWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._recoverPetArray = [];
            if (PetRecoverWindow._instance != null) 
            {
                throw new Error(" PetRecoverWindow 单例 ");
            }
            this.setSize(360, 345);
            title = Language.getString(54112);
            titleHeight = 30;
            this.addEventListener(flash.events.Event.CHANGE, this.onSelectedChangeHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            Dispatcher.addEventListener(EventName.PetRecoverModelSuccess, this.onPetRecoverModelSuccessHandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1;
            super.createChildren();
            ObjCreate.createBg(17, 65, 174, 201, this);
            this._bgSprite = new Object /* flash.display.Sprite */();
            this._bgSprite.x = 20;
            this._bgSprite.y = 72;
            this.addChild(this._bgSprite);
            this._bg = new Object /* flash.display.Bitmap */();
            this._bgSprite.addChild(this._bg);
            with ({}) 
            {
            };
            "PetPreviewBg.swf".load(
#error('pushwith') {}
            , 
#error('pushwith') {}
            .onLoaded = (arg1: SWFInfo): void
            {
                _bg.bitmapData = arg1.bitmapData;
                _bg.width = 167;
                _bg.height = 185;
                return;
            })
            this._petModel = new PetModelPanel();
            this._petModel.isShowTurnBtn = false;
            this._petModel.x = 110;
            this._petModel.y = 210;
            this.addChild(this._petModel);
            ObjCreate.createBg(15, 40, 180, 23, this, ImagesConst.RbListHeader);
            ObjCreate.createTextField(Language.getString(54113), 40, 45, 175, 20, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            this._nameTxt = ObjCreate.createTextField("", 100, 45, 175, 20, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            ObjCreate.createBg(195, 45, 145, 220, this);
            this._petListPanel = new TilePanel();
            this._petListPanel.setStyle("cellRenderer", PetRecoverListRenderer);
            this._petListPanel.setSize(135, 225);
            this._petListPanel.columnWidth = 135;
            this._petListPanel.rowHeight = 45;
            this._petListPanel.move(200, 50);
            this.addChild(this._petListPanel);
            this._tipTxt = ObjCreate.createTextField("", 25, 280, 300, 20, this, GlobalStyle.textFormat5);
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._tipTxt.htmlText = Language.getString(54114);
            this._okBtn = ObjCreate.createGButton(Language.getString(54112), 130, 305, 90, 30, this, "ButtonSpecial");
            this._okBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            return;
        }

        /* internal  */updateByPetInfo(arg1: SPetInfo): void
        {
            if (arg1) 
            {
                this._nameTxt.htmlText = PetUtil.getPetName(arg1);
                this._petModel.updateModelByCode(arg1.changeCode);
            }
            return;
        }

        /* internal  */onSelectedChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._selectPetInfo = this._petListPanel.selectedItem as SPetInfo;
            if (this._selectPetInfo) 
            {
                this.updateByPetInfo(this._selectPetInfo);
            }
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._selectPetInfo) 
            {
                if (PetUtil.isBattled(this._selectPetInfo)) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54115));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetRecoverModel, this._selectPetInfo.uid));
            }
            return;
        }

        public onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (this.isHide == false && this._selectPetInfo) 
            {
                loc1 = arg1.data as SPetUpdate;
                if (loc1.updateType != 4) 
                {
                    this.updateDataProvider();
                }
                else if (this._selectPetInfo.uid == loc1.petInfo.uid) 
                {
                    this._selectPetInfo = loc1.petInfo;
                }
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this.updateDataProvider();
            return;
        }

        /* internal  */updateDataProvider(): void
        {
            this._recoverPetArray = Cache.instance.pet.petInfos.filter(this.filterRecoverPet);
            this._petListPanel.dataProvider = new DataProvider(this._recoverPetArray);
            this._petListPanel.selectedIndex = 0;
            if (this._petListPanel.dataProvider.length == 0) 
            {
                this.clear();
            }
            return;
        }

        /* internal  */filterRecoverPet(arg1: SPetInfo, arg2: int, arg3: Array<any>): Boolean
        {
            return !(arg1.code == arg1.changeCode) && arg1.isBorrow == false;
        }

        /* internal  */onPetRecoverModelSuccessHandler(arg1: DataEvent): void
        {
            this.updateDataProvider();
            MsgManager.showRollTipsMsg(Language.getString(54116));
            return;
        }

        /* internal  */clear(): void
        {
            this._nameTxt.text = "";
            return;
        }

        public static get instance(): PetRecoverWindow
        {
            if (PetRecoverWindow._instance == null) 
            {
                PetRecoverWindow._instance = new PetRecoverWindow();
            }
            return PetRecoverWindow._instance;
        }

        /* internal */ /* var */_bgSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_petModel: PetModelPanel;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_okBtn: GButton;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_petListPanel: TilePanel;

        /* internal */ /* var */_selectPetInfo: SPetInfo;

        /* internal */ /* var */_recoverPetArray: Array<any>;

        /* internal */ static /* var */_instance: PetRecoverWindow;
    }
